const fs = require('fs')
const path = require('path')
const targetDirPath = process.env.PWD || process.cwd && process.cwd()

// 分析文件目录
const findTree = (filePath) => {
    let treeObj = {}
    if (fs.existsSync(filePath)) {
        let stat = fs.statSync(filePath, { encoding: 'UTF-8' })
        treeObj.name = path.basename(filePath)
        treeObj.path = filePath.split(path.sep).join('/')
        if (treeObj.name.indexOf('.wxml') > -1) {
            treeObj.isPage = true
            treeObj.pagePath = filePath.replace(path.resolve(filePath, '../../../') + path.sep, '').replace('.wxml', '').split(path.sep).join('/')
        }

        if (stat.isDirectory()) {
            treeObj.type = 1

            treeObj.children = []
            let files = fs.readdirSync(filePath, { encoding: 'UTF-8' })
            for (let fileName of files) {
                treeObj.children.push(findTree(filePath + path.sep + fileName))
            }
        } else {
            treeObj.type = 0
        }

    }
    return treeObj
}

// 抽取pages
const findPages = (fileTree, buildConfigJson, findInPackage = false) => {
    let pages = []
    if (fileTree && fileTree.type == 1) {
        for (let tree of fileTree.children) {
            pages = pages.concat(findPages(tree, buildConfigJson, findInPackage))
        }
    } else if (fileTree.type == 0 && fileTree.isPage) {
        let { componentPath = [], packagePath = [] } = buildConfigJson
        let mergePath = [...componentPath]
        if (!findInPackage) {
            mergePath = mergePath.concat(packagePath)
        }
        if (mergePath && mergePath.length) {
            let flag = true
            for (let p of mergePath) {
                if (fileTree.path.indexOf(p) > -1) {
                    flag = false
                    break
                }
            }
            if (flag) {
                pages.push(fileTree.pagePath)
            }
        } else {
            pages.push(fileTree.pagePath)
        }
    }
    return pages
}

// 抽取分包
const findSubpackagesDir = (fileTree, buildConfigJson) => {
    let dir = []
    if (fileTree && fileTree.type == 1) {
        let { packagePath } = buildConfigJson
        let isPackage = false
        for (let p of packagePath) {
            if (fileTree.path.indexOf(p) > -1) {
                isPackage = true
                break
            }
        }
        if (isPackage) {
            dir = dir.concat([fileTree])
            return dir
        }
        for (let tree of fileTree.children) {
            dir = dir.concat(findSubpackagesDir(tree, buildConfigJson))
        }
    }
    return dir
}
const findSubpackages = (fileTree, buildConfigJson) => {
    let subpackages = []
    let subpackagesDir = findSubpackagesDir(fileTree, buildConfigJson)
    for (let package of subpackagesDir) {
        subpackages.push({
            // root: package.path.replace(targetDirPath, ''),
            root: package.path.replace(path.resolve(package.path, '../../') + path.sep, '').split(path.sep).join('/'),
            pages: findPages(package, buildConfigJson, true)
        })
    }
    return subpackages
}

exports.findTree = findTree
exports.findPages = findPages
exports.findSubpackages = findSubpackages
